package com.thudojsc.canvasengine.touch;

import android.view.MotionEvent;

import com.thudojsc.canvasengine.util.GenericPool;

public class TouchEvent {
	public final static int ACTION_DOWN = MotionEvent.ACTION_DOWN;
	public final static int ACTION_UP = MotionEvent.ACTION_UP;
	public final static int ACTION_MOVE = MotionEvent.ACTION_MOVE;
	public final static int ACTION_OUTSIDE = MotionEvent.ACTION_OUTSIDE;
	public final static int ACTION_CANCEL = MotionEvent.ACTION_CANCEL;
	
	
	private static final TouchEventPool TOUCH_EVENT_POOL = new TouchEventPool();
	
	protected int mPointerID;
	protected int mTouchX;
	protected int mTouchY;
	protected int mAction;
	protected MotionEvent mEvent;
	protected long mTouchMoment;
	
	public static TouchEvent obtain(final int pTouchX, final int pTouchY, final int pPointerID, 
			final int pAction, final MotionEvent pMotionEvent) {
		final TouchEvent cloner = TOUCH_EVENT_POOL.obtain();
		cloner.set(pTouchX, pTouchY, pPointerID, pAction, pMotionEvent);
		cloner.mTouchMoment = System.currentTimeMillis();
		return cloner;
	}
	
	public void set(final int pTouchX, final int pTouchY, final int pPointerID, 
			final int pAction, final MotionEvent pMotionEvent) {
		this.mTouchX = pTouchX;
		this.mTouchY = pTouchY;
		this.mAction = pAction;
		this.mPointerID = pPointerID;
		this.mEvent = pMotionEvent;
	}

	public void recycle() {
		TOUCH_EVENT_POOL.recycle(this);
	}
	
	public int getX() {
		return mTouchX;
	}
	
	public int getY() {
		return mTouchY;
	}
	
	public boolean isActionDown() {
		return mAction == ACTION_DOWN;
	}
	
	public boolean isActionUp() {
		return mAction == ACTION_UP;
	}
	
	public boolean isActionMove() {
		return mAction == ACTION_MOVE;
	}
	
	public boolean isActionOutside() {
		return mAction == ACTION_OUTSIDE;
	}
	
	public boolean isActionCancel() {
		return mAction == ACTION_CANCEL;
	}
	
	public int getAction() {
		return mAction;
	}
	
	public int getPointerID() {
		return mPointerID;
	}
	
	public long getTouchMoment() {
		return mTouchMoment;
	}
	
	private static final class TouchEventPool extends GenericPool<TouchEvent> {

		@Override
		protected TouchEvent onAllocatePoolItem() {
			return new TouchEvent();
		}
		
	}
}
